package com.sto.stoapp.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sto.stoapp.activity.R;

/**
 * 重写LinearLayout
 * 
 * @author mgf
 * 
 */
@SuppressLint("NewApi")
public class StoSelectLinearLayout extends LinearLayout {

	private TextView title;

	private ImageView flagView;

	private boolean iSelected = false;

	public StoSelectLinearLayout(Context context, AttributeSet attrs,
			int defStyle) {
		super(context, attrs, defStyle);
		init(context);
	}

	public StoSelectLinearLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public StoSelectLinearLayout(Context context) {
		super(context);
		init(context);
	}

	private void init(Context context) {
		this.setOrientation(LinearLayout.HORIZONTAL);
		this.setGravity(Gravity.CENTER_VERTICAL);
		initTextView(context);
		initImageView(context);
	}

	private void initTextView(Context context) {
		title = new TextView(context);
		title.setLayoutParams(new LinearLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 1.0f));
		title.setSingleLine();
		title.setTextSize(context.getResources().getDimension(R.dimen.eighteen));
		title.setTextColor(Color.BLACK);
		this.addView(title);
	}

	private void initImageView(Context context) {
		flagView = new ImageView(context);
		flagView.setLayoutParams(new LinearLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		flagView.setImageResource(R.drawable.select_btn);
		this.addView(flagView);
	}

	/**
	 * 设置选中状态
	 * 
	 * @param res
	 */
	public void setSelectStatus(boolean res) {
		iSelected = res;
		if (flagView != null) {
			if (res) {
				flagView.setImageResource(R.drawable.selected_btn);
			} else {
				flagView.setImageResource(R.drawable.select_btn);
			}
		}
	}

	public ImageView getFlagView() {
		return flagView;
	}

	/**
	 * 得到是否选中状态
	 * 
	 * @return
	 */
	public boolean getSelectStatus() {
		return iSelected;
	}

	/**
	 * 设置标题
	 * 
	 * @param id
	 */
	public void setTitle(int id) {
		if (title != null) {
			title.setText(id);
		}
	}
	/**
	 * 设置标题
	 * 
	 * @param id
	 */
	public void setTitle(String id) {
		if (title != null) {
			title.setText(id);
		}
	}
	/**
	 * 设置标题字体大小
	 * 
	 * @param size
	 */
	public void setTitleSize(int unit, float size) {
		if (title != null) {
			title.setTextSize(unit, size);
		}
	}
}
